using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentData;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Model;
using System.Data;

namespace AdverseEventReportSystem.Repository 
{
    public class AdverseEventModeRecordRepository : BaseRepository<AdverseEventModeRecord>
    {

        #region 构造函数

        private static AdverseEventModeRecordRepository _instance;
        public static AdverseEventModeRecordRepository GetInstance()
        {
            if (_instance == null)
            {
                _instance = new AdverseEventModeRecordRepository();
            }
            return _instance;
        }

        #endregion

        #region Add

        public int Add(AdverseEventModeRecord entity, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            int Id = context.Insert<AdverseEventModeRecord>("AdverseEventModeRecord", entity)
                    .AutoMap(x => x.ID)
                    .ExecuteReturnLastId<int>();
            return Id;
        }

        #endregion

        #region Update

        public int Update(AdverseEventModeRecord entity, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            int rowsAffected = context.Update<AdverseEventModeRecord>("AdverseEventModeRecord", entity)
                    .AutoMap(x => x.ID)
                    .Where(x => x.ID)
                    .Execute();
            return rowsAffected;
        }

        #endregion

        #region 获取单个实体 通过主键ID

        /// <summary>
        /// 获取单个实体 通过主键ID
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <param name="context">数据库连接对象</param>
        /// <returns></returns>
        public virtual AdverseEventModeRecord GetQuerySingleByID(int id, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            Object[] objects = new Object[1];
            objects[0] = id;
            string sqlStr = "SELECT * FROM AdverseEventModeRecord WHERE ID=@0";
            AdverseEventModeRecord obj = GetQuerySingle(sqlStr, context, objects);
            return obj;
        }

        #endregion

        #region 删除操作  通过ID

        /// <summary>
        /// 删除操作  通过ID
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <param name="context">数据库连接对象</param>
        /// <returns></returns>
        public virtual int DelByID(int id, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            Object[] objects = new Object[1];
            objects[0] = id;
            string sqlStr = "DELETE FROM AdverseEventModeRecord WHERE ID=@0";
            return Del(sqlStr, context, objects);
        }

        #endregion

        #region 根据不良事件ID删除

        /// <summary>
        /// 根据事件ID删除
        /// </summary>
        /// <param name="eventID">不良事件ID</param>
        /// <param name="context">数据库连接对象</param>
        /// <returns></returns>
        public virtual int DelByEventID(int eventID, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            Object[] objects = new Object[1];
            objects[0] = eventID;
            string sqlStr = "DELETE FROM AdverseEventModeRecord WHERE EventID=@0";
            return Del(sqlStr, context, objects);
        }

        #endregion

        #region 获取实体DataTable

        /// <summary>
        /// 根据不良事件ID跟类型
        /// </summary>
        /// <param name="typeID">不良事件ID</param>
        /// <param name="typeID">事件类型ID</param>
        /// <param name="context">数据库连接对象</param>
        /// <returns></returns>
        public virtual DataTable GetAdverseEventModeRecordTable(int eventID, int typeID, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            Object[] objects = new Object[2];
            objects[0] = eventID;
            objects[1] = typeID;
            string sqlStr = "select a.TypeID,a.ID as ModeID,case when b.ModeName is null then a.ModeName else b.ModeName end ModeName,case when b.InputType is null then a.InputType else b.InputType end InputType,case when b.Options is null then a.Options else b.Options end Options,b.Value from AdverseEventMode a left join AdverseEventModeRecord b on a.ID = b.ModeID and b.EventID = @0 WHERE a.TypeID = @1 Order By a.Sort";
            return GetDataTable(sqlStr, context, objects);
        }

        #endregion

    }
}





